package cn.fuxi2022.qiniu.utils;

import cn.fuxi2022.qiniu.properties.QiNiuProperties;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.File;
import java.util.UUID;

/**
 * 七牛云工具类
 * 注意：Zone.zone2()为华南机房，如果不对需要调整
 */
public class QiNiuUtils {


    public static final String IMAGE_CONFIG_PATH = "image/config/";
    public static final String IMAGE_USER_PATH = "image/user/";

    /**
     * 实现单个文件上传
     *
     * @param file     上传的文件
     * @param filePath 上传的路径（可以使用当前类设置，也可以自定义（后面携带/））
     * @param type     文件的后缀名
     * @return 文件链接
     */
    public static String uploadFileFromQiniu(File file, String filePath, String type) {

        // 根据UUID设置文件名
        // 生成随机UUID
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(QiNiuProperties.ACCESSKEY, QiNiuProperties.SECRETKEY);
        String upToken = auth.uploadToken(QiNiuProperties.BUCKET);
        try {
            Response response = uploadManager.put(file, filePath + uuid + type, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return QiNiuProperties.CDN + putRet.key;

        } catch (QiniuException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /**
     * 实现单个文件上传
     *
     * @param file     上传的文件
     * @param filePath 上传的路径（可以使用当前类设置，也可以自定义（后面携带/））
     * @param type     文件的后缀名
     * @return 文件链接
     */
    public static String uploadFileFromQiniu(byte[] file, String filePath, String type) {

        // 根据UUID设置文件名
        // 生成随机UUID
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(QiNiuProperties.ACCESSKEY, QiNiuProperties.SECRETKEY);
        String upToken = auth.uploadToken(QiNiuProperties.BUCKET);
        try {
            Response response = uploadManager.put(file, filePath + uuid + type, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return QiNiuProperties.CDN + putRet.key;

        } catch (QiniuException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /**
     * 删除图片
     *
     * @param url 删除的文件路径
     * @return true成功 false失败
     */
    public static boolean deleteFileFromQiniu(String url) {
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        Auth auth = Auth.create(QiNiuProperties.ACCESSKEY, QiNiuProperties.SECRETKEY);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(QiNiuProperties.BUCKET, url.substring(QiNiuProperties.CDN.length()));
            return true;
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            ex.printStackTrace();
        }
        return false;
    }
}